package com.xjgc.webservice;

import com.xjgc.msi.exo.wsclient.cwgk.budget_cancel.SapBudgetCancelFfPortType;
import com.xjgc.msi.exo.wsclient.cwgk.budget_ctrl_check.SapCheckYsFfPortType;
import com.xjgc.msi.exo.wsclient.cwgk.budget_write.SapBudgetCtrlWriteFfPortType;
import com.xjgc.msi.exo.wsclient.cwgk.sync_paid.SyncPaidFromERPServicePortType;
import com.xjgc.msi.exo.wsclient.plm.tcm.ITcWebServiceManager;
import com.xjgc.msi.exo.wsclient.sap.mom2erp.ZXJMOM2ERP;
import com.xjgc.msi.exo.wsclient.sap.zjk_back01.ZxjZjkBack01V2;
import com.xjgc.msi.exo.wsclient.sap.zwsficoreimbursementbill.ZWSFICOREIMBURSEMENTBILL;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.xml.ws.Holder;

/**
 * @author: 小逗号
 * @date: 2023-12-1
 * @description:
 * @since:
 */
@Slf4j
@Component
public class ExoWebServiceCall {
    @Resource
    private SapCheckYsFfPortType sapCheckYsFfPortType;
    @Resource
    private SapBudgetCancelFfPortType sapBudgetCancelFfPortType;
    @Resource
    private SapBudgetCtrlWriteFfPortType sapBudgetCtrlWriteFfPortType;
    @Resource
    private SyncPaidFromERPServicePortType syncPaidFromERPServicePortType;
    //private NBZFDJServicePortType nbzfdjServicePortType;
    /**/
    //private ZXJMESBACK zxjmesback;
    @Resource
    private ZxjZjkBack01V2 zxjZjkBack01V2;
    @Resource
    private ZXJMOM2ERP zxjmom2ERP;
    @Resource
    private ZWSFICOREIMBURSEMENTBILL zwsficoreimbursementbill;
    /**/
    @Resource
    private ITcWebServiceManager tcWebServiceManager;

    /**
     * 调用WebService服务接口
     *
     * @param webService 服务名(cxf-client注册的client.id)
     * @param taskCode   任务名(分发任务的常量)
     * @param serialNo   流水号
     * @param requestXml xml
     * @return
     */
    public String call(String webService, String taskCode, String serialNo, String requestXml) {
        log.info("调用外部WebService服务:{}-{}==>{}", webService, taskCode, serialNo);
        try {
            //财务管控-预算执行检查
            if (webService.equals(SapCheckYsFfPortType.class.getSimpleName())) {
                String responseXml = sapCheckYsFfPortType.checkYsFf(requestXml);
                return responseXml;
            }
            //财务管控-预算执行释放
            if (webService.equals(SapBudgetCancelFfPortType.class.getSimpleName())) {
                String responseXml = sapBudgetCancelFfPortType.ctrlWriteFf(requestXml);
                return responseXml;
            }
            //财务管控-预算执行行数回写
            if (webService.equals(SapBudgetCtrlWriteFfPortType.class.getSimpleName())) {
                String responseXml = sapBudgetCtrlWriteFfPortType.ctrlWriteFf(requestXml);
                return responseXml;
            }
            //财务管控-支付入池(/付款申请)
            if (webService.equals(SyncPaidFromERPServicePortType.class.getSimpleName())) {
                String responseXml = syncPaidFromERPServicePortType.receivePaidData(requestXml);
                return responseXml;
            }

            //SAP-实时传输
            if (webService.equals(ZxjZjkBack01V2.class.getSimpleName())) {
                String responseXml = zxjZjkBack01V2.zxjZJKBACK01(taskCode, serialNo, requestXml, "1-1");
                return responseXml;
            }
            //SAP-定时传输
            //SAP-MOM系列接口
            if (webService.equals(ZXJMOM2ERP.class.getSimpleName())) {
                Holder<String> output1 = new Holder<>();
                Holder<String> output2 = new Holder<>();
                Holder<String> output3 = new Holder<>();
                Holder<String> output4 = new Holder<>();
                zxjmom2ERP.zxjMOM2ERP(taskCode, serialNo, requestXml, "1-1", output1, output2, output3, output4);
                String task = output1.value;
                String ser = output2.value;
                String response = output3.value;
                String page = output4.value;
                //log.info("out0:{},out1:{},out2:{},out3:{}", output1.value, output2.value, output3.value, output4.value);
                //log.info("接收到sap返回数据[{}]-流水号{}-->{}", TASK_CODE, serialNo, sapBackXml);
                return response;
            }
            //SAP-创建会计凭证接口
            if (webService.equals(ZWSFICOREIMBURSEMENTBILL.class.getSimpleName())) {
                String response = zwsficoreimbursementbill.zfmFicoReimbursementBill(requestXml);
                return response;
            }
            //PLM-主数据接口
            if (webService.equals(ITcWebServiceManager.class.getSimpleName())) {
                if ("sap2TCCommpart".equals(taskCode)) {
                    String response = tcWebServiceManager.sap2TCCommpart(requestXml);
                    return response;
                }
                return "PLM主数据接口,任务名" + taskCode + ",未注册!";
            }

        } catch (Exception e) {
            e.printStackTrace();
            return webService + "接口调用失败!" + e.getMessage();
        }
        return "外部服务:" + webService + ",中间库尚未注册成功!";
    }

}
